import { DataType } from "./type";
import type { TableProps } from "antd";
import { Space, Popconfirm, Tag } from "antd";

export default function getColumns(
  handleRewind: (id: number) => void,
  handleDelete: (id: number) => void
): TableProps<DataType>["columns"] {
  return [
    {
      title: "项目名称",
      dataIndex: "project_name",
      minWidth: 120,
    },
    {
      title: "快照状态",
      dataIndex: "snapshot_status",
      render: (value) => {
        return (
          <Tag color={["warning", "processing", "success"][value]}>
            {["已打包", "已部署", "运行中"][value]}
          </Tag>
        );
      },
      minWidth: 100,
    },
    {
      title: "快照描述",
      dataIndex: "snapshot_description",
      minWidth: 150,
    },
    {
      title: "构建时长",
      dataIndex: "build_duration",
      minWidth: 100,
      render: (value) => {
        return value ? `${(value / 1000).toFixed(2)}s` : "--";
      },
    },
    {
      title: "部署时长",
      dataIndex: "deploy_duration",
      minWidth: 100,
      render: (value) => {
        return value ? `${(value / 1000).toFixed(2)}s` : "--";
      },
    },
    {
      title: "构建时间",
      dataIndex: "build_time",
      minWidth: 100,
    },
    {
      title: "部署时间",
      dataIndex: "deploy_time",
      minWidth: 100,
      render: (value) => {
        return value ? value : "--";
      },
    },
    {
      title: "创建时间",
      dataIndex: "create_time",
      width: 160,
    },
    {
      title: "修改时间",
      dataIndex: "modify_time",
      width: 160,
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 120,
      render: (_, record) => (
        <Space size="middle">
          {record.snapshot_status === 1 && (
            <Popconfirm
              title="提示"
              description="确定要上线该版本吗？"
              okText="确定"
              cancelText="取消"
              onConfirm={() => handleRewind(record.id)}
            >
              <a>上线</a>
            </Popconfirm>
          )}
          <Popconfirm
            title="提示"
            description="确定要删除吗？"
            okText="确定"
            cancelText="取消"
            onConfirm={() => handleDelete(record.id)}
          >
            <a>删除</a>
          </Popconfirm>
        </Space>
      ),
    },
  ];
}
